/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JProductAnalysisPanel.java
 *
 * Created on 2010-6-5, 9:32:09
 */

package cn.ekuma.epos.analysis.product;

import cn.ekuma.data.dao.bean.IKeyed;
import cn.ekuma.epos.businesslogic.ProductUtil;
import cn.ekuma.epos.core.util.ProductSwingUtil;
import cn.ekuma.epos.datalogic.I_DataLogicCustomers;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.epos.db.table.erp.subject.I_SubjectProduct;
import cn.ekuma.epos.module.ERP_UI_Swing_Model;
import cn.ekuma.rabbit.spi.I_ModelPart;

import com.openbravo.beans.JCalendarDialog;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.data.gui.ListQBFModelNumber;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.LinkQBFParameter;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.data.loader.ui.JQBFParametersPanel;
import com.openbravo.data.loader.ui.QBFCompareEnumListCellRenderer;
import com.openbravo.data.user.EditorCreator;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;
import com.openbravo.pos.base.BeanFactoryApp;
import com.openbravo.pos.base.BeanFactoryException;
import com.openbravo.pos.base.I_AppView;
import com.openbravo.bean.erp.OperationProductCategory;
import com.openbravo.bean.erp.subject.Subject;
import com.openbravo.bean.infobean.CustomerInfo;
import com.openbravo.bean.Location;
import com.openbravo.bean.LocationLayout;
import com.openbravo.bean.Product;
import com.openbravo.bean.ProductCategory;
import com.openbravo.bean.SharedProduct;
import com.openbravo.pos.forms.JPanelView;
import com.openbravo.pos.panels.JCustomerFinder;
import com.openbravo.pos.panels.JDropProductByLocationStatePanel;
import com.openbravo.pos.panels.JProductFinder;
import com.openbravo.pos.panels.QuickTimerFilter;
import com.openbravo.pos.panels.QuickTimerHFilter;
import com.openbravo.pos.panels.event.CustomerChangedListener;
import com.openbravo.pos.panels.event.ProductCategoryChangedListener;
import com.openbravo.pos.panels.event.ProductChangedListener;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;
import com.openbravo.pos.panels.event.TimeCycleChangedListener;
import com.openbravo.pos.panels.model.ProductTableModel;
import com.openbravo.pos.panels.navigator.JCustomerNavigator;
import com.openbravo.pos.panels.navigator.JLocationLayoutNavigator;
import com.openbravo.pos.panels.navigator.JOperationProductCategoryNavigator;
import com.openbravo.pos.panels.navigator.JProductCategoryNavigator;
import com.openbravo.pos.panels.navigator.JSubjectNavigator;
import com.openbravo.pos.panels.navigator.event.LocationLayoutChangedListener;
import com.openbravo.pos.panels.navigator.event.OperationProductCategoryChangedListener;
import com.openbravo.pos.panels.navigator.event.SubjectChangedListener;

import java.awt.CardLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.util.Date;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JPanel;

import net.sourceforge.jtimepiece.TimeUtil;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.ImageIcon;
import javax.swing.ListCellRenderer;
import javax.swing.JToggleButton;

/**
 *
 * @author Administrator
 */
public class JProductAnalysisPanel extends javax.swing.JPanel implements JPanelView, BeanFactoryApp,EditorCreator, TimeCycleChangedListener,I_ModelPart<ERP_UI_Swing_Model>{
    private I_DataLogicERP  dlSales;
    private I_DataLogicCustomers dlCustomers;
    private I_DataLogicSystem dlSystem;
    private AnalysisModelManager manager;
    private JProdcutHistoryView view;
    private JProductTableView table;
    private ComboBoxValModel m_LocationsModel;
    private CustomerInfo customerInfo;

    private String showPanel;
	private QuickTimerFilter timerFilter;
	
	Object[] beforeFilterParas;
	private JProductFinder productFinder;
	
    LinkQBFParameter subjectLinkParameter=new LinkQBFParameter(I_SubjectProduct.TABLENAME,I_SubjectProduct.PRODUCT);
	private JButton btnNewButton_3;
	private QuickTimerHFilter quickTimerHFilter;
    
    /** Creates new form JProductAnalysisPanel */
    public JProductAnalysisPanel() {
    	initComponents();
    	inputCode.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				m_jBarcode1ActionPerformed(null);
			}
    	});
    	comboBox.setModel(ListQBFModelNumber.getMandatoryNumber());
    	comboBox.setRenderer(new QBFCompareEnumListCellRenderer());
    	stockNum = new JTextField();
    	panel_1.add(stockNum);
    	stockNum.setColumns(6);
    	
    	tbIncludeTransaction = new JToggleButton("");
    	tbIncludeTransaction.setIcon(new ImageIcon(JProductAnalysisPanel.class.getResource("/com/openbravo/images/cakes3d.png")));
    	tbIncludeTransaction.setPreferredSize(new Dimension(49, 25));
    	tbIncludeTransaction.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			manager.setInclundTransaction(tbIncludeTransaction.isSelected());
    		}
    	});
    	tbIncludeTransaction.setSelectedIcon(new ImageIcon(JProductAnalysisPanel.class.getResource("/com/openbravo/images/cakes3d.png")));
    	panel_1.add(tbIncludeTransaction);
    	
    	btnNewButton_2 = new JButton(AppLocal.getIntString("label.Subject"));
    	btnNewButton_2.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			cl1.show(jNavigator, "subject");
    		}
    	});
    	jNavigatorToolBar.add(btnNewButton_2);
    	
    	btnNewButton_3 = new JButton(AppLocal.getIntString("label.locationLayout"));
    	btnNewButton_3.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			cl1.show(jNavigator, "locationLayout");
    		}
    	});
    	jNavigatorToolBar.add(btnNewButton_3);
    	
    	m_jLocation.addActionListener(new ActionListener(){
    		
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					locationLayoutNavigator.loadData(((Location)m_jLocation.getSelectedItem()).getKey());
					manager.fireLocationChanged((Location)m_jLocation.getSelectedItem());
				} catch (BasicException ex) {
					new MessageInf(ex).show(JProductAnalysisPanel.this);
					return;
				}
			}
    	});
    	
    	quickTimerHFilter.addTimeCycleChangedListener(new TimeCycleChangedListener(){

			@Override
			public void timeCycleChanged(TimeCycleChangedEvent e) {
				fireNewProductQuery(e.getStartDate(),e.getEndDate());
			}
    		
    	});
    }
    
    private void fireNewProductQuery(Date startDate,Date endDate){
    	if(beforeFilterParas==null)
			try {
				beforeFilterParas=(Object[]) createValue();
			} catch (BasicException e) {
				new MessageInf(e).show(JProductAnalysisPanel.this);
				return;
			}
		try{
    	     manager.query(startDate, endDate, beforeFilterParas);
           } catch (BasicException ex) {
            new MessageInf(ex).show(this);
         }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.CardLayout());
        add(jPanel1, java.awt.BorderLayout.CENTER);
    	
    	panel = new JPanel();
    	add(panel, BorderLayout.NORTH);
    	panel.setLayout(new BorderLayout(0, 0));
    	
    	        jToolBar1 = new javax.swing.JToolBar();
    	        panel.add(jToolBar1, BorderLayout.NORTH);
    	        
    	        timerFilter=new QuickTimerFilter();
    	        panel.add(timerFilter,BorderLayout.CENTER);
    	        timerFilter.addTimeCycleChangedListener(this);
    	        jBack = new javax.swing.JButton();
    	        jtxtCustomer = new javax.swing.JTextField();
    	        btnCustomer = new javax.swing.JButton();
    	        jLabel3 = new javax.swing.JLabel();
    	        jTxtStartDate = new javax.swing.JTextField();
    	        btnDateStart = new javax.swing.JButton();
    	        jLabel4 = new javax.swing.JLabel();
    	        jTxtEndDate = new javax.swing.JTextField();
    	        btnDateEnd = new javax.swing.JButton();
    	        m_jLocation = new javax.swing.JComboBox();
    	        jButton1 = new javax.swing.JButton();
    	        jButton5 = new javax.swing.JButton();
    	        
    	                jToolBar1.setRollover(true);
    	                
    	                        jBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1leftarrow.png"))); // NOI18N
    	                        jBack.setFocusable(false);
    	                        jBack.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    	                        jBack.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    	                        jBack.addActionListener(new java.awt.event.ActionListener() {
    	                            public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                                jBackActionPerformed(evt);
    	                            }
    	                        });
    	                        jToolBar1.add(jBack);
    	                                
    	                                button_4 = new JButton("");
    	                                button_4.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/inbox.png")));
    	                                button_4.addActionListener(new ActionListener() {
    	                                	public void actionPerformed(ActionEvent e) {
    	                                		jAddShardProduct();
    	                                	}
    	                                });
    	                                button_4.setPreferredSize(new Dimension(50, 20));
    	                                jToolBar1.add(button_4);
    	                        
    	                                jtxtCustomer.setPreferredSize(new java.awt.Dimension(100, 25));
    	                                jToolBar1.add(jtxtCustomer);
    	                                
    	                                        btnCustomer.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/kuser.png"))); // NOI18N
    	                                        btnCustomer.setFocusPainted(false);
    	                                        btnCustomer.setFocusable(false);
    	                                        btnCustomer.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    	                                        btnCustomer.setMargin(null);
    	                                        btnCustomer.setPreferredSize(new java.awt.Dimension(55, 25));
    	                                        btnCustomer.setRequestFocusEnabled(false);
    	                                        btnCustomer.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    	                                        btnCustomer.addActionListener(new java.awt.event.ActionListener() {
    	                                            public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                                                btnCustomerActionPerformed(evt);
    	                                            }
    	                                        });
    	                                        jToolBar1.add(btnCustomer);
    	                                        
    	                                                jLabel3.setText(AppLocal.getIntString("Label.StartDate")); // NOI18N
    	                                                jToolBar1.add(jLabel3);
    	                                                
    	                                                        jTxtStartDate.setMinimumSize(new java.awt.Dimension(6, 15));
    	                                                        jTxtStartDate.setPreferredSize(new java.awt.Dimension(150, 22));
    	                                                        jToolBar1.add(jTxtStartDate);
    	                                                        
    	                                                                btnDateStart.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
    	                                                                btnDateStart.setFocusable(false);
    	                                                                btnDateStart.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    	                                                                btnDateStart.setPreferredSize(new java.awt.Dimension(55, 25));
    	                                                                btnDateStart.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    	                                                                btnDateStart.addActionListener(new java.awt.event.ActionListener() {
    	                                                                    public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                                                                        btnDateStartActionPerformed(evt);
    	                                                                    }
    	                                                                });
    	                                                                jToolBar1.add(btnDateStart);
    	                                                                
    	                                                                        jLabel4.setText(AppLocal.getIntString("Label.EndDate")); // NOI18N
    	                                                                        jToolBar1.add(jLabel4);
    	                                                                        
    	                                                                                jTxtEndDate.setPreferredSize(new java.awt.Dimension(150, 22));
    	                                                                                jToolBar1.add(jTxtEndDate);
    	                                                                                
    	                                                                                        btnDateEnd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/date.png"))); // NOI18N
    	                                                                                        btnDateEnd.setFocusable(false);
    	                                                                                        btnDateEnd.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    	                                                                                        btnDateEnd.setPreferredSize(new java.awt.Dimension(55, 25));
    	                                                                                        btnDateEnd.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    	                                                                                        btnDateEnd.addActionListener(new java.awt.event.ActionListener() {
    	                                                                                            public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                                                                                                btnDateEndActionPerformed(evt);
    	                                                                                            }
    	                                                                                        });
    	                                                                                        jToolBar1.add(btnDateEnd);
    	                                                                                        
    	                                                                                                m_jLocation.setPreferredSize(new java.awt.Dimension(150, 21));
    	                                                                                                jToolBar1.add(m_jLocation);
    	                                                                                                
    	                                                                                                        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/1rightarrow.png"))); // NOI18N
    	                                                                                                        jButton1.setFocusable(false);
    	                                                                                                        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    	                                                                                                        jButton1.setPreferredSize(new java.awt.Dimension(55, 25));
    	                                                                                                        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    	                                                                                                        jButton1.addActionListener(new java.awt.event.ActionListener() {
    	                                                                                                            public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                                                                                                                jButton1ActionPerformed(evt);
    	                                                                                                            }
    	                                                                                                        });
    	                                                                                                        jToolBar1.add(jButton1);
    	                                                                                                        
    	                                                                                                                jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/search.png"))); // NOI18N
    	                                                                                                                jButton5.setFocusable(false);
    	                                                                                                                jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    	                                                                                                                jButton5.setPreferredSize(new java.awt.Dimension(55, 25));
    	                                                                                                                jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    	                                                                                                                jButton5.addActionListener(new java.awt.event.ActionListener() {
    	                                                                                                                    public void actionPerformed(java.awt.event.ActionEvent evt) {
    	                                                                                                                        jButton5ActionPerformed(evt);
    	                                                                                                                    }
    	                                                                                                                });
    	                                                                                                                jToolBar1.add(jButton5);
    	                                                                                                                
    	                                                                                                                btnNewButton_1 = new JButton("");
    	                                                                                                                btnNewButton_1.addActionListener(new ActionListener() {
    	                                                                                                                	public void actionPerformed(ActionEvent e) {
    	                                                                                                                		m_jResert1ActionPerformed(e);
    	                                                                                                                	}
    	                                                                                                                });
    	                                                                                                                btnNewButton_1.setIcon(new ImageIcon(JProductAnalysisPanel.class.getResource("/com/openbravo/images/reload.png")));
    	                                                                                                                jToolBar1.add(btnNewButton_1);
    	                                                                                                                
    	                                                                                                                jNavigatorToolBar = new JPanel();
    	                                                                                                                panel.add(jNavigatorToolBar, BorderLayout.EAST);
    	                                                                                                                jNavigatorToolBar.setLayout(new BoxLayout(jNavigatorToolBar, BoxLayout.LINE_AXIS));
    	                                                                                                                
    	                                                                                                                button = new JButton(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/search.png")));
    	                                                                                                                button.addActionListener(new ActionListener() {
    	                                                                                                                	public void actionPerformed(ActionEvent e) {
    	                                                                                                                		jNavigator.setVisible(!jNavigator.isVisible());
    	                                                                                                                		if(jNavigator.isVisible()){
    	                                                                                                        					cl1.show(jNavigator, "QBFPanel");
    	                                                                                                        				}
    	                                                                                                                	}
    	                                                                                                                });
    	                                                                                                                
    	                                                                                                                panel_1 = new JPanel();
    	                                                                                                                jNavigatorToolBar.add(panel_1);
    	                                                                                                                
    	                                                                                                                inputCode = new JTextField();
    	                                                                                                                inputCode.setPreferredSize(new Dimension(100, 21));
    	                                                                                                                panel_1.add(inputCode);
    	                                                                                                                
    	                                                                                                                button_3 = new JButton(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/apply.png")));
    	                                                                                                                button_3.addActionListener(new ActionListener() {
    	                                                                                                                	public void actionPerformed(ActionEvent e) {
    	                                                                                                                		m_jBarcode1ActionPerformed(e);
    	                                                                                                                	}
    	                                                                                                                });
    	                                                                                                                panel_1.add(button_3);
    	                                                                                                                
    	                                                                                                                comboBox = new JComboBox();
    	                                                                                                                comboBox.setPreferredSize(new Dimension(80, 21));
    	                                                                                                                panel_1.add(comboBox);
    	                                                                                                                jNavigatorToolBar.add(button);
    	                                                                                                                
    	                                                                                                                button_1 = new JButton(AppLocal.getIntString("label.customer"));
    	                                                                                                                button_1.addActionListener(new ActionListener() {
    	            	                                                                                                	public void actionPerformed(ActionEvent e) {
    	            	                                                                                                		cl1.show(jNavigator, "customer");
    	            	                                                                                                	}
    	            	                                                                                                });
    	                                                                                                                jNavigatorToolBar.add(button_1);
    	                                                                                                                
    	                                                                                                                button_2 = new JButton(AppLocal.getIntString("label.prodcategory"));
    	                                                                                                                button_2.addActionListener(new ActionListener() {
    	            	                                                                                                	public void actionPerformed(ActionEvent e) {
    	            	                                                                                                		cl1.show(jNavigator, "category");
    	            	                                                                                                	}
    	            	                                                                                                });
    	                                                                                                                jNavigatorToolBar.add(button_2);
    	                                                                                                                
    	                                                                                                                btnNewButton = new JButton(AppLocal.getIntString("label.operationProductCategory"));
    	                                                                                                                btnNewButton.addActionListener(new ActionListener() {
    	                                                                                                                	public void actionPerformed(ActionEvent e) {
    	                                                                                                                		cl1.show(jNavigator, "operationCategory");
    	                                                                                                                	}
    	                                                                                                                });
    	                                                                                                                jNavigatorToolBar.add(btnNewButton);
    	
    	jNavigator = new JPanel();
    	jNavigator.setPreferredSize(new Dimension(240, 10));
    	add(jNavigator, BorderLayout.EAST);
    	jNavigator.setLayout(new CardLayout(0, 0));
    	
    	basePanel = new JPanel();
    	jNavigator.add(basePanel, "QBFPanel");
    	basePanel.setLayout(new BorderLayout(0, 0));
    	
    	quickTimerHFilter=new QuickTimerHFilter();
    	basePanel.add(quickTimerHFilter,BorderLayout.SOUTH);
    	
    	JQBFParametersPanel qbfPanel=new JQBFParametersPanel(){

			@Override
			public void doQBFParametersFilter(QBFParameters paras) {
				query(paras);
			}
        };
        qbfPanel.setShowFieldCount(6);
        qbfPanel.setFields(ProductTableModel.getQBFFields("P"));
        basePanel.add(qbfPanel, BorderLayout.CENTER);
    	
    }// </editor-fold>//GEN-END:initComponents

   

	protected void jAddShardProduct() {
		if (productId!= null){
			 try {
	            	dlSales.saveSharedProduct(new SharedProduct(productId));
	            } catch (BasicException ex) {
	                new MessageInf(ex).show(this);
	      };
		}
	}

	protected void m_jBarcode1ActionPerformed(ActionEvent e) {
    	String sCode=inputCode.getText();
    	if(sCode.isEmpty()){
            return ;
         }
         boolean isSeachModel=false;
         // precondicion: sCode != null
         try {
             Product oProduct=null;
             if(sCode.matches("^[~～][A-Z0-9]+")){
             	isSeachModel=true;
                   java.util.List<Product> prods=dlSales.listProductByMnemonic(sCode.substring(1));
                   if(prods!=null&&prods.size()>0)
                       if(prods.size()==1)
                          oProduct=prods.get(0);
                       else
                          oProduct=getProductFinder().setProductList(prods);
             }else{
                 oProduct = ProductSwingUtil.getProductInfo(this, dlSales, sCode.trim());
             }
             
             if (oProduct == null) {                  
                 Toolkit.getDefaultToolkit().beep(); 
                 if(!isSeachModel)
                 	new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.noproduct")).show(this);
                
             } else {
            	 if(beforeFilterParas==null)
         				beforeFilterParas=(Object[]) createValue();
 
         			// Product
         			beforeFilterParas[12]=QBFCompareEnum.COMP_IN;
         			beforeFilterParas[13]=new String[]{oProduct.getID()};
         			reQuery(); 
            	 
             }
         } catch (BasicException eData) {  
             new MessageInf(eData).show(this);           
         }
		
	}

	public JProductFinder getProductFinder() {
        if(productFinder==null)
            productFinder=JProductFinder.getInstance(this, dlSales,JProductFinder.PRODUCT_ALL);
        return productFinder;
    }

	private void btnCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCustomerActionPerformed
        JCustomerFinder finder = JCustomerFinder.getCustomerFinder(this, dlSales);
        finder.search(null);
        finder.setVisible(true);
       // try {
            customerInfo=finder.getSelectedCustomer();
            jtxtCustomer.setText(finder.getSelectedCustomer() == null
                    ? null
                    : finder.getSelectedCustomer().getName());// dlSales.loadCustomer(customerInfo.getId()).toString());
       // } catch (BasicException e) {
          //  MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.cannotfindcustomer"), e);
          //  msg.show(this);
       // }
}//GEN-LAST:event_btnCustomerActionPerformed

    private void btnDateStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateStartActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
            jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(date));
        }
}//GEN-LAST:event_btnDateStartActionPerformed

    private void btnDateEndActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDateEndActionPerformed
        Date date;
        try {
            date = (Date) Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());
        } catch (BasicException e) {
            date = null;
        }
        date = JCalendarDialog.showCalendarTimeHours(this, date);
        if (date != null) {
            jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(date));
        }
}//GEN-LAST:event_btnDateEndActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
        	beforeFilterParas=(Object[])createValue();
            reQuery();
        } catch (BasicException ex) {
            new MessageInf(ex).show(this);
        }
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        JProductFilterQBFDialog orderTypeFiler = JProductFilterQBFDialog.getOrderFilterQBFDialog(this, dlSales,dlCustomers);
        orderTypeFiler.setVisible(true);
        if (orderTypeFiler.isOK()) {
            try {
            	beforeFilterParas=new Object[22];
                Object[] temp=(Object[]) orderTypeFiler.createValue();
                for(int i=0;i<16;i++)
                	beforeFilterParas[i]=temp[i];
                //Operation Cateory
                beforeFilterParas[16]=QBFCompareEnum.COMP_NONE;
                beforeFilterParas[17]=null;      
                
              //stock num
                Double s=getDoubleValue();
                beforeFilterParas[18] = s == null ? QBFCompareEnum.COMP_NONE : comboBox.getSelectedItem();
                beforeFilterParas[19] = s;
                
                timerFilter.resert();
                reQuery();
            } catch (BasicException ex) {
               new MessageInf(ex).show(this);
            }
        }
}//GEN-LAST:event_jButton5ActionPerformed

    private void jBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBackActionPerformed
        CardLayout cl = (CardLayout)(jPanel1.getLayout());
        cl.show(jPanel1, "table");
        showPanel="table";
        jBack.setVisible(false);
    }//GEN-LAST:event_jBackActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCustomer;
    private javax.swing.JButton btnDateEnd;
    private javax.swing.JButton btnDateStart;
    private javax.swing.JButton jBack;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextField jTxtEndDate;
    private javax.swing.JTextField jTxtStartDate;
    private javax.swing.JTextField jtxtCustomer;
    private javax.swing.JComboBox m_jLocation;
    private JPanel panel;
    private JPanel jNavigator;
    private JPanel jNavigatorToolBar;
    private JButton button;
    private JButton button_1;
    private JButton button_2;
	private CardLayout cl1;
	private JProductCategoryNavigator productCategoryNavigator;
    // End of variables declaration//GEN-END:variables
	private JCustomerNavigator customerNavigator;
	private AppView app;
	private JOperationProductCategoryNavigator operationProductCategoryNavigator;
	private JButton btnNewButton;
	private JPanel panel_1;
	private JTextField inputCode;
	private JButton button_3;
	private JComboBox comboBox;
	private JTextField stockNum;
	private JButton btnNewButton_1;
	private JButton button_4;
	protected String productId;
	private JSubjectNavigator subjectNavigator;
	private JButton btnNewButton_2;

    public String getTitle() {
        return "";
    }

    public void activate() throws BasicException {
        
        List<Location> locationList=null;
        try {
            locationList=dlSales.list(Location.class);
        } catch (BasicException ex) {
            new MessageInf(ex).show(this);
        }
        m_LocationsModel = new ComboBoxValModel(locationList);
        m_jLocation.setModel(m_LocationsModel); // para que lo refresque
        if(locationList.size()>0)
            m_jLocation.setSelectedIndex(0);
        if(!app.isCustomerView()){
        	customerNavigator.loadData();
            productCategoryNavigator.loadData();
            operationProductCategoryNavigator.loadData();
            if(locationList.size()>0){
            	locationLayoutNavigator.loadData(locationList.get(0).getKey());
            	manager.fireLocationChanged(locationList.get(0));
            }
         }
    }

    public boolean deactivate() {
        return true;
    }

    public JComponent getComponent() {
        return this;
    }

    JLocationLayoutNavigator locationLayoutNavigator;
    private JToggleButton tbIncludeTransaction;
    private JPanel basePanel;
    
    public void init(I_AppView app1) throws BeanFactoryException {
    	this.app=(AppView) app1;
        manager= new AnalysisModelManager(dlSales);
        manager.addPropertyChangeListener( new PropertyChangeListener(){
            public void propertyChange(PropertyChangeEvent evt) {
                if(evt.getPropertyName().equals(AnalysisModelManager.PROPERTY_DialyProductID)){
                     CardLayout cl = (CardLayout)(jPanel1.getLayout());
                     cl.show(jPanel1, "view");
                     showPanel="view";
                    jBack.setVisible(true);
                }
            }
        });
        m_jLocation.addItemListener(new ItemListener(){

			@Override
			public void itemStateChanged(ItemEvent e) {
				manager.fireLocationChanged((Location) e.getItem());
			}
        });
        tbIncludeTransaction.setSelected(manager.isInclundTransaction());
        view=new JProdcutHistoryView(manager);
        jPanel1.add(view,"view");
        table=new JProductTableView(manager);
        jPanel1.add(table,"table");
        CardLayout cl = (CardLayout)(jPanel1.getLayout());
        cl.show(jPanel1, "table");
        jBack.setVisible(false);
        showPanel="table";
        cl1=(CardLayout)(jNavigator.getLayout());
        jNavigator.setVisible(false);
        if(app.isCustomerView()){
        	this.customerInfo=app.getCustomerInfo();
        	jtxtCustomer.setText(customerInfo.getName());
        	jtxtCustomer.setEditable(false);
        	jButton5.setEnabled(false);
        	btnCustomer.setEnabled(false);
        	button.setEnabled(false);
            button_1.setEnabled(false);
            button_2.setEnabled(false);
            btnNewButton.setEnabled(false);
            btnNewButton_2.setEnabled(false);
            btnNewButton_3.setEnabled(false);
        }else{
        	productCategoryNavigator=new JProductCategoryNavigator(dlSales);
        	productCategoryNavigator.addProductCategoryChangedListener(new ProductCategoryChangedListener(){

				@Override
				public void valueSelectChanged(ProductCategory obj) {
					if(beforeFilterParas==null)
						try {
							beforeFilterParas=(Object[]) createValue();
						} catch (BasicException e) {
							new MessageInf(e).show(JProductAnalysisPanel.this);
							return;
						}
						
						// Product Cateory
						beforeFilterParas[14]=QBFCompareEnum.COMP_IN;
						beforeFilterParas[15]=new String[]{obj.getID()};
						reQuery(); 
					
				}
        		
        	});
        	jNavigator.add(productCategoryNavigator,"category");
        	
        	customerNavigator=new JCustomerNavigator(dlSales);
            customerNavigator.addCustomerChangedListener(new CustomerChangedListener(){

				@Override
				public void valueSelectChanged(CustomerInfo obj) {
					if(beforeFilterParas==null)
						try {
							beforeFilterParas=(Object[]) createValue();
						} catch (BasicException e) {
							new MessageInf(e).show(JProductAnalysisPanel.this);
							return;
						}
						
						//Customer
						customerInfo=obj;
						beforeFilterParas[4] = QBFCompareEnum.COMP_IN;
						beforeFilterParas[5] = new String[]{customerInfo.getId()};
						jtxtCustomer.setText(customerInfo.getName());
						reQuery(((Location)m_jLocation.getSelectedItem()).getID(),obj.getId());   	
				}
            	
            });
            jNavigator.add(customerNavigator, "customer");
            
            operationProductCategoryNavigator =new JOperationProductCategoryNavigator(dlSales);
            operationProductCategoryNavigator.addOperationProductCategoryChangedListener(new OperationProductCategoryChangedListener(){

				@Override
				public void valueSelectChanged(OperationProductCategory obj) {
					 if(beforeFilterParas==null)
							try {
								beforeFilterParas=(Object[]) createValue();
							} catch (BasicException e) {
								new MessageInf(e).show(JProductAnalysisPanel.this);
								return;
							}
							//Operation Cateory
							beforeFilterParas[16]=QBFCompareEnum.COMP_EQUALS;
					        beforeFilterParas[17]=obj.getID(); 
							reQuery(); 
					
				}
            	
            });
        	jNavigator.add(operationProductCategoryNavigator,"operationCategory");
        	
        	subjectNavigator =new JSubjectNavigator(dlSales);
        	subjectNavigator.addSubjectChangedListener(new SubjectChangedListener(){

				@Override
				public void valueSelectChanged(Subject obj) {
					if(beforeFilterParas==null)
						try {
							beforeFilterParas=(Object[]) createValue();
						} catch (BasicException e) {
							new MessageInf(e).show(JProductAnalysisPanel.this);
							return;
						}
						
						//Time
						Object startdate = obj.getStartDate();
				        Object enddate = obj.getEndDate();

				        beforeFilterParas[0] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
				        beforeFilterParas[1] = startdate;
				        beforeFilterParas[2] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
				        beforeFilterParas[3] = enddate;
						
				       // Product
						subjectLinkParameter.setQbfParameter(new QBFParameter(I_SubjectProduct.SUBJECT).equal(obj.getId()));
						beforeFilterParas[12]=QBFCompareEnum.COMP_IN;
						beforeFilterParas[13]=subjectLinkParameter;
						reQuery(); 
				}
        	});
        	jNavigator.add(subjectNavigator,"subject");
        	
        	locationLayoutNavigator=new JLocationLayoutNavigator(dlSales);
        	locationLayoutNavigator.addLocationLayoutChangedListener(new LocationLayoutChangedListener(){

				@Override
				public void valueSelectChanged(LocationLayout obj) {
					if(beforeFilterParas==null)
						try {
							beforeFilterParas=(Object[]) createValue();
						} catch (BasicException e) {
							new MessageInf(e).show(JProductAnalysisPanel.this);
							return;
						}
						beforeFilterParas[20] = QBFCompareEnum.COMP_EQUALS;
						beforeFilterParas[21] = obj.getKey();
						reQuery();
				}
        	});
        	jNavigator.add(locationLayoutNavigator,"locationLayout");
        }
        stockNum.setText(null);
        
        manager.addProductChangedListener(new ProductChangedListener(){

			@Override
			public void baseBeanChanged(IKeyed<String> productID, Product o) {
				productId=productID.getKey();
			}
        });
    }
    
    private void reQuery(){
        try{
     	manager.query(beforeFilterParas);
        table.peferredJXTableColumn();
        CardLayout cl = (CardLayout)(jPanel1.getLayout());
        cl.show(jPanel1, "table");
        showPanel="table";
        jBack.setVisible(false);
         if(beforeFilterParas[1]!=null)
         	jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(beforeFilterParas[1]));
         else
         	jTxtStartDate.setText(null);
         if(beforeFilterParas[3]!=null)
         	jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(beforeFilterParas[3]));
         else
         	jTxtEndDate.setText(null);
      } catch (BasicException ex) {
         new MessageInf(ex).show(this);
     }
     }
    
    private void reQuery(String location,String customer){
    	try{
         	manager.query(location,customer,beforeFilterParas);
            table.peferredJXTableColumn();
            CardLayout cl = (CardLayout)(jPanel1.getLayout());
            cl.show(jPanel1, "table");
            showPanel="table";
            jBack.setVisible(false);
             if(beforeFilterParas[1]!=null)
             	jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(beforeFilterParas[1]));
             else
             	jTxtStartDate.setText(null);
             if(beforeFilterParas[3]!=null)
             	jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(beforeFilterParas[3]));
             else
             	jTxtEndDate.setText(null);
          } catch (BasicException ex) {
             new MessageInf(ex).show(this);
         }
    }
    
    
    QBFParameter localPara=new QBFParameter("S.LOCATION");
    protected void query(QBFParameters paras) {
    	paras.and(this.localPara.equal(((Location)m_jLocation.getSelectedItem()).getID()));
    	try{
         	manager.query(paras);
            table.peferredJXTableColumn();
            CardLayout cl = (CardLayout)(jPanel1.getLayout());
            cl.show(jPanel1, "table");
            showPanel="table";
            jBack.setVisible(false);
             if(beforeFilterParas[1]!=null)
             	jTxtStartDate.setText(Formats.TIMESTAMP.formatValue(beforeFilterParas[1]));
             else
             	jTxtStartDate.setText(null);
             if(beforeFilterParas[3]!=null)
             	jTxtEndDate.setText(Formats.TIMESTAMP.formatValue(beforeFilterParas[3]));
             else
             	jTxtEndDate.setText(null);
          } catch (BasicException ex) {
             new MessageInf(ex).show(this);
         }
		
	}
    
    
    public Object getBean() {
        return this;
    }

	protected void m_jResert1ActionPerformed(ActionEvent e) {
		beforeFilterParas=null;
		timerFilter.resert();
		jTxtStartDate.setText(null);
		jTxtEndDate.setText(null);
		comboBox.setSelectedItem(QBFCompareEnum.COMP_NONE);
		stockNum.setText(null);
		if(!app.isCustomerView()){
			customerInfo = null;
			jtxtCustomer.setText(null);
		}
	}
    
    
    public Object createValue() throws BasicException {
        Object[] afilter = new Object[22];
          // Date range
        Object startdate = Formats.TIMESTAMP.parseValue(jTxtStartDate.getText());
        Object enddate = Formats.TIMESTAMP.parseValue(jTxtEndDate.getText());

        afilter[0] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
        afilter[1] = startdate;
        afilter[2] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
        afilter[3] = enddate;

        //Customer
        if (customerInfo == null) {
            afilter[4] = QBFCompareEnum.COMP_NONE;
            afilter[5] = null;
        } else {
            afilter[4] = QBFCompareEnum.COMP_IN;
            afilter[5] = new String[]{customerInfo.getId()};
        }

        //Location
        if(m_jLocation.getSelectedItem()==null){
            afilter[6] = QBFCompareEnum.COMP_NONE;
            afilter[7] = null;
        }else{
            afilter[6] = QBFCompareEnum.COMP_EQUALS;
            afilter[7] = ((Location)m_jLocation.getSelectedItem()).getID();
        }
        // orderType
            afilter[8]=QBFCompareEnum.COMP_NONE;
            afilter[9]=null;

        // User
            afilter[10]=QBFCompareEnum.COMP_NONE;
            afilter[11]=null;

       // Product

            afilter[12]=QBFCompareEnum.COMP_NONE;
            afilter[13]=null;

        // Product Cateory
            afilter[14]=QBFCompareEnum.COMP_NONE;
            afilter[15]=null;
            
       //Operation Cateory
            afilter[16]=QBFCompareEnum.COMP_NONE;
            afilter[17]=null;
      
      //stock num
            Double s=getDoubleValue();
            afilter[18] = s == null ? QBFCompareEnum.COMP_NONE : comboBox.getSelectedItem();
            afilter[19] = s;
            
       //Location Layout
            afilter[20]=QBFCompareEnum.COMP_NONE;
            afilter[21]=null;      
            
       return afilter;
    }

	
public Double getDoubleValue() {
        String text = stockNum.getText();
        if (text == null || text.equals("")) {
            return null; 
        } else {
            try {
                return Double.parseDouble(text);
            } catch (NumberFormatException e) {
                return null;
            }
        }
    }

@Override
public void timeCycleChanged(TimeCycleChangedEvent e) {
	
	if(beforeFilterParas==null)
		try {
			beforeFilterParas=(Object[]) createValue();
		} catch (BasicException ex) {
			new MessageInf(ex).show(this);
			return;
		}
	  // Date range
    Object startdate = e.getStartDate();
    Object enddate = e.getEndDate();

    beforeFilterParas[0] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
    beforeFilterParas[1] = startdate;
    beforeFilterParas[2] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
    beforeFilterParas[3] = enddate;
    reQuery();   
}

@Override
public String getModelName() {
	return ERP_UI_Swing_Model.MODEL_NAME;
}

@Override
public void setModel(ERP_UI_Swing_Model mode) {
	dlSales=mode.getDLERP();
    dlCustomers=mode.getDLCustomer();
    dlSystem=mode.getDLSystem();
	
}
}
